package 栈;

import java.util.*;

// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {
    }

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
}

public class _589_N_叉树的前后层序遍历 {

    ///////////////////////前序遍历/////////////////////
    static List<Integer> preResult = new ArrayList<>();

    public static List<Integer> preorder(Node root) {
        preorder1(root);
        return preResult;
    }

    public static void preorder1(Node node) {
        if (node == null) return;
        preResult.add(node.val);
        if (node.children == null) return;
        for (Node child : node.children) {
            preorder1(child);
        }
    }

    public static List<Integer> preorderByIterator(Node root) {
        LinkedList<Integer> res = new LinkedList<>();
        if (root == null) {
            return res;
        }

        Node temp = root;
        Stack<Node> stack = new Stack<>();
        stack.push(temp);
        while (!stack.isEmpty()) {
            temp = stack.pop();
            res.add(temp.val);
            if (temp.children != null) {
                Collections.reverse(temp.children);
                stack.addAll(temp.children);
            }
        }

        return res;
    }


    //////////////////////////后序遍历////////////////////
    static List<Integer> postResult = new ArrayList<>();

    public static List<Integer> postorder(Node root) {
        postorder1(root);
        return postResult;
    }

    public static void postorder1(Node node) {
        if (node == null) return;
        if (node.children != null) {
            for (Node child : node.children) {
                postorder1(child);
            }
        }
        postResult.add(node.val);
    }

    public static List<Integer> postorderByIterator(Node root) {
        LinkedList<Integer> res = new LinkedList<>();
        if (root == null) {
            return res;
        }

        Deque<Node> stack = new ArrayDeque<>();
        stack.addLast(root);
        while (!stack.isEmpty()) {
            Node node = stack.removeLast();
            res.addFirst(node.val);
            if (node.children == null) continue;
            for (int i = 0; i < node.children.size(); i++) {
                stack.addLast(node.children.get(i));
            }
        }
        return res;

    }


    /**
     * 层序遍历
     * 输入：root = [1,null,3,2,4,null,5,6]
     * 输出：[[1],[3,2,4],[5,6]]
     */
    static List<List<Integer>> levelResult = new ArrayList<>();

    public static List<List<Integer>> levelOrder(Node root) {
        if (root == null) return levelResult;
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        List<Integer> tmp = new ArrayList<>();
        levelResult.add(tmp);
        int size = 1;
        while (!queue.isEmpty()) {
            Node node = queue.poll();
            levelResult.get(levelResult.size() - 1).add(node.val);

            size--;

            if (node.children != null) {
                queue.addAll(node.children);
//                for (Node child : node.children) {
//                    queue.offer(child);
//                }
            }

            if (size == 0 && queue.size() > 0) {
                size = queue.size();
                levelResult.add(new ArrayList<>());
            }
        }

        return levelResult;
    }

    public static void main(String[] args) {
        List<Node> nodeList1 = new ArrayList<>();
        nodeList1.add(new Node(5));
        nodeList1.add(new Node(6));
        List<Node> nodeList = new ArrayList<>();
        nodeList.add(new Node(3, nodeList1));
        nodeList.add(new Node(2));
        nodeList.add(new Node(4));

        System.out.println("前序遍历（递归）:" + preorder(new Node(1, nodeList)));
        System.out.println("前序遍历（迭代）:" + preorderByIterator(new Node(1, nodeList)));
        System.out.println("层序遍历（迭代）:" + levelOrder(new Node(1, nodeList)));
        System.out.println("后序遍历（递归）:" + postorder(new Node(1, nodeList)));
        System.out.println("后序遍历（迭代）:" + postorderByIterator(new Node(1, nodeList)));
    }
}
